package com.cy.api;


import java.lang.annotation.*;
import java.lang.reflect.Field;

//自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Entity{
    String value() default ""
;}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
@interface ID{
    String value() default "";
}

@Entity("t_user")
class User{
    @ID("uid")
    private Integer id;

}
public class AnnotationTests {


    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Annotation[] annotations =Class.forName("com.cy.api.User").getAnnotations();
        String s1 = annotations[0].toString();
        System.out.println(s1);//获取了t_user注解

        //1,获取user类上的entity注解中的内容
        //1.1获取User类的字节码对象
        Class<?> targetCls=User.class;
        System.out.println(targetCls);
        //1.2获取User类上的Entity注解
        Entity entity = targetCls.getAnnotation(Entity.class);
        System.out.println(entity);
        //1.3获取Entity注解nalue属性值
        String entityValue1=entity.value();
        System.out.println(entityValue1);
        String entityValue =  User.class.getAnnotation(Entity.class).value();
        System.out.println(entityValue);
        System.out.println();

        //2.获取User类中id属性上注解ID的value属性值
        //2.1获取User类中的id属性
        Class<?> targetClss = User.class;
        Field f = targetClss.getDeclaredField("id");
        //2.2获取id属性上ID注解
        ID id=f.getAnnotation(ID.class);
        //2.3获取id注解中value属性的值
        String idValie= id.value();
        System.out.println(idValie);

        String value = User.class.getDeclaredField("id").getAnnotation(ID.class).value();
        System.out.println(value);







    }

}
